﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Skybot.Arcgis.Client.TileLayerExpand;

namespace BingMap数据采集
{
    public partial class Form1 : Form
    {
        /// <summary>
        /// 采集主要對像
        /// </summary>
        GetBingMapImages getBingMapImages = null;
        public Form1()
        {

            InitializeComponent();

            Timer timer = new Timer();
            timer.Interval = 500;
            timer.Tick += Timer1_Tick;
            timer.Start();
            LayerType.SelectedIndex = 0;
            //初始化圖層
            LayerType_SelectedIndexChanged(LayerType, new EventArgs());

            //初始化 保存對話框
            folderBrowserDialog.ShowNewFolderButton = true;

            //初始化採集資料
            getBingMapImages = new GetBingMapImages() { Pic = pictureBox1, Parent = this };

        }

        /// <summary>
        /// 采集进度查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Timer1_Tick(object sender, EventArgs e)
        {
            if (folderBrowserDialog.SelectedPath.Length > 0)
            {
                label4.Text = "保存到:";
                ttPath.Text = folderBrowserDialog.SelectedPath;
            }
            label1.Text = string.Format("共:{0}个,当前{1},进度:{2}", SingleCollectionState.Instance.Count.ToString(), SingleCollectionState.Instance.CurrentIndex.ToString(), SingleCollectionState.Instance.Percentage.ToString());
            notifyIcon1.Text = "工作狀態:\r\n" + label1.Text;
            //K
            if (SingleCollectionState.Instance.Count * 1564.581147 / 1024 > 1024)
            {
                //M
                if (SingleCollectionState.Instance.Count * 1564.581147 / 1024 / 1024 > 1024)
                {
                    //G
                    if (SingleCollectionState.Instance.Count * 1564.581147 / 1024 / 1024 / 1024 > 1024)
                    {
                        //T
                        if (SingleCollectionState.Instance.Count * 1564.581147 / 1024 / 1024 / 1024 / 1024 > 1024)
                        {

                            labSpace.Text = "        估计大小:" + SingleCollectionState.Instance.Count * 1564.581147 / 1024 / 1024 / 1024 / 1024 / 1024 + "EB";
                            labSpace.Text += "\r\n估计占用空间:" + (SingleCollectionState.Instance.Count * 1564.581147 / 1024 / 1024 / 1024 / 1024 / 1024) * 3.170141473 + "EB";

                        }
                        else
                        {
                            labSpace.Text = "估计大小:" + SingleCollectionState.Instance.Count * 1564.581147 / 1024 / 1024 / 1024 / 1024 + "T";
                            labSpace.Text += "\r\n估计占用空间:" + (SingleCollectionState.Instance.Count * 1564.581147 / 1024 / 1024 / 1024 / 1024) * 3.170141473 + "T";

                        }

                    }
                    else
                    {
                        labSpace.Text = "估计大小:" + SingleCollectionState.Instance.Count * 1564.581147 / 1024 / 1024 / 1024 + "G";
                        labSpace.Text += "\r\n估计占用空间:" + (SingleCollectionState.Instance.Count * 1564.581147 / 1024 / 1024 / 1024) * 3.170141473 + "G";
                    }

                }
                else
                {
                    labSpace.Text = "估计大小:" + SingleCollectionState.Instance.Count * 1564.581147 / 1024 / 1024 + "M";
                    labSpace.Text += "\r\n估计占用空间:" + (SingleCollectionState.Instance.Count * 1564.581147 / 1024 / 1024) * 3.170141473 + "M";

                }
            }
            else
            {
                labSpace.Text = "估计大小:" + SingleCollectionState.Instance.Count * 1564.581147 / 1024 + "K";
                labSpace.Text += "\r\n估计占用空间:" + (SingleCollectionState.Instance.Count * 1564.581147 / 1024) * 3.170141473 + "K";

            }

        }
        /// <summary>
        /// 异常Boox
        /// </summary>
        public TextBox ttExceptionTextBox
        {
            get
            {
                return textBox1;
            }
        }

        /// <summary>
        /// 開始採集
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            //改變初始化值
            if (txtBaseLevel.Text.Trim().Split(',').Length > 0)
            {
                //得到當前的資料
                getBingMapImages.BaseLevel = (from str in txtBaseLevel.Text.Trim().Split(',')
                                              select str.Trim()).ToList();
            }
            if (ttPath.Text != null)
            {
                if (ttPath.Text.Length > 0)
                {
                    try
                    {
                        if (System.IO.Directory.Exists(ttPath.Text))
                        {

                            getBingMapImages.BaseUrl = ttPath.Text;
                        }
                        else
                        {
                            MessageBox.Show("目錄" + ttPath.Text + "不存在");
                            return;
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                        return;
                    }
                }
                else
                {
                    MessageBox.Show("請先指定保存路徑\r\n請點擊操作界面上的:\"地圖保存路徑選擇\"(蓝色字体)");
                    return;
                }
            }

            getBingMapImages.GetIMages();
            this.WindowState = FormWindowState.Minimized;
        }
        /// <summary>
        /// 選擇改變
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LayerType_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox comBobx = (ComboBox)sender;
            ///圖層類型
            string layerType = "http://r3.tiles.ditu.live.com/tiles/r";

            if (comBobx.SelectedItem != null)
            {
                ///圖類型
                ///Road='r',
                ///http://r3.tiles.ditu.live.com/tiles/r
                /// <summary>
                /// 衛星圖 a 
                /// http://t1.tiles.virtualearth.net/tiles/a
                /// </summary>
                //Aerial='a',
                /// <summary>
                /// 帶標注的衛星圖 h
                /// http://t1.tiles.virtualearth.net/tiles/h
                /// </summary>
                ///AerialWithLabels='h'
                /// </summary>

                switch (comBobx.SelectedItem.ToString())
                {
                    case "Road":
                        layerType = "http://r3.tiles.ditu.live.com/tiles/r";
                        break;

                    case "Aerial":
                        layerType = "http://t1.tiles.virtualearth.net/tiles/a";
                        break;

                    case "AerialWithLabels":
                        layerType = "http://t1.tiles.virtualearth.net/tiles/h";
                        break;
                }
            }

            //指定圖的屬性

            SingleCollectionState.Instance.CurrentGetUrl = layerType;

        }

        /// <summary>
        ///支援資訊
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://blog.csdn.net/daonidedie");
        }

        /// <summary>
        /// 必应地图图片系统 Link
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://acnchen.spaces.live.com/blog/cns!9321DA57338F36BA!3425.entry");
        }

        private void 參數設置_Enter(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 調用文件夾選擇
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void label4_Click(object sender, EventArgs e)
        {
            folderBrowserDialog.ShowNewFolderButton = true;
            folderBrowserDialog.ShowDialog();

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Hide();
                this.notifyIcon1.Visible = true;
            }

        }

        /// <summary>
        /// 雙擊事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
        }

        /// <summary>
        /// 轉換成ARCGIS緩存地圖文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 轉換成ARCGIS緩存地圖文件_Click(object sender, EventArgs e)
        {

            folderBrowserDialogArcgis.ShowNewFolderButton=true;
            folderBrowserDialogArcgis.ShowDialog();

            if (folderBrowserDialogArcgis.SelectedPath == "")
            {
                return;
            }
            new System.Threading.Thread(new System.Threading.ThreadStart(() =>
            {

                foreach (var file in new System.IO.DirectoryInfo(folderBrowserDialog.SelectedPath).GetFiles("r*"))
                {
                    将一个图片转换成ARCGIS緩存地圖图片文件(file.FullName, folderBrowserDialogArcgis.SelectedPath);
                }


            })).Start(); ;


        }

        /// <summary>
        /// 将一个图片转换成ARCGIS緩存地圖图片文件 并保存
        /// </summary>
        /// <param name="FullImageName">全文件名</param>
        /// <param name="TargetDir">目标保存基目录</param>
        void 将一个图片转换成ARCGIS緩存地圖图片文件(String FullImageName, string TargetDir)
        {

            //看看是不是包含目录
            if (!TargetDir.Contains("/layers/_alllayers"))
            {
                TargetDir += "/layers/_alllayers";
            }

            string QuadKey = System.Text.RegularExpressions.Regex.Match( System.IO.Path.GetFileName(FullImageName).Replace(System.IO.Path.GetExtension(FullImageName), ""),@"\d{1,}").Value;

            var x = 0;
            var y = 0;
            var z = 0;
            TileSystem.QuadKeyToTileXY(QuadKey, out x, out y, out z);
            if (z >=16)
            {
                return;
            }
            //定议放大变小的级别
            //L0
            string L = "L" + z.ToString("d2");

            //定议 Tile 的行 
            //R00000db8
            string R = "R" + y.ToString("X8").ToLower();

            //定议 Tile 的列 
            // C00001369.png
            string C = "C" + x.ToString("X8").ToLower() + System.IO.Path.GetExtension(FullImageName);

            //如果  定议放大变小的级别 目录不存在
            if (!System.IO.Directory.Exists(TargetDir + "/" + L))
            {
                System.IO.Directory.CreateDirectory(TargetDir + "/" + L);
            }

            //如果  定议 Tile 的列 目录不存在
            if (!System.IO.Directory.Exists(TargetDir + "/" + L + "/" + R))
            {
                System.IO.Directory.CreateDirectory(TargetDir + "/" + L + "/" + R);
            }

            //开始写入保存文件
            System.IO.File.Copy(FullImageName, (TargetDir + "/" + L + "/" + R + "/" + C), true);

        }



    }
}
